#include "PlayManager.h"
#include <cocostudio/cocostudio.h>
#include "../../Data/GameData.h"
#include "../Level/LevelManager.h"
#include "../../Support/GameSupport.h"

#include "../../Objects/Ghost/Play/GhostAdjust.h"


using namespace cocostudio;

EGameStatus PlayManager::m_EGameStatus = EGameStatus::kGhostStatus_None;

Scene* PlayManager::createScene()
{
	auto scene = Scene::create();
	auto layer = PlayManager::create();
    
	scene->addChild(layer);
	return scene;
}

bool PlayManager::init()
{
	if (!Layer::init())
	{
		return false;
	}

	setKeypadEnabled(true);
	m_screenSize = Director::getInstance()->getVisibleSize();
	initialize();

	return true;
}

void PlayManager::initialize()
{
	this->removeAllChildren();

	initBackground();
	initLayer();
    GameData* gamedata = GameData::getInstance();
    std::map<int, LevelData>::iterator iter;
    switch (LevelManager::s_mode)
	{
	case kMode_Easy:
        iter = gamedata->easyMode.find(LevelManager::s_ID);
        
       
		break;
	case kMode_Normal:
        iter = gamedata->normalMode.find(LevelManager::s_ID);
        
		break;
	case kMode_Hard:
        iter = gamedata->hardMode.find(LevelManager::s_ID);
        
		break;
	}
    s_playType = iter->second.Type;
    
    if (s_playType == EPlayType::kType_Adjust)
	{
        
     //   m_loadingBar->runAction(RepeatForever::create(Sequence::create(DelayTime::create(1.0f), func, NULL)));

		OpenPlayAdjustLayer();
	}
    else if (s_playType == EPlayType::kType_Select)
	{
        //m_PlaySelectLayer->initialize()
		OpenPlaySelectLayer();
	}
}

void PlayManager::initBackground()
{
	std::string s;
	switch (LevelManager::s_mode) 
	{
	case EGameMode::kMode_Easy: s = "bg_easy.png"; break;
	case EGameMode::kMode_Normal: s = "bg_normal.png"; break;
	case EGameMode::kMode_Hard: s = "bg_hard.png"; break;
	}
	Sprite* background = Sprite::create(s.c_str());

	float scaleX = m_screenSize.width / background->getContentSize().width;
	float scaleY = m_screenSize.height / background->getContentSize().height;

	background->setScale(scaleX, scaleY);
	background->setAnchorPoint(Vec2(0.5f, 0.5f));
	background->setPosition(Vec2(m_screenSize.width / 2, m_screenSize.height / 2));
	this->addChild(background);
}

void PlayManager::initLayer()
{
	auto node = CSLoader::getInstance()->createNode("GameScene.csb");
	float scale = m_screenSize.height / node->getContentSize().height;
	node->setScale(scale);

    m_PlaySelectLayer = new PlaySelectLayer(m_screenSize);
    m_PlaySelectLayer->initialize(node->getChildByName(std::string(GameSupport::getTextPlayLayer(EPlayLayer::kPlayLayer_Select))));
    m_PlayAdjustLayer = new PlayAdjustLayer(m_screenSize);
    m_PlayAdjustLayer->initialize(node->getChildByName(std::string(GameSupport::getTextPlayLayer(EPlayLayer::kPlayLayer_Adjust))));
	
    //node->getChildByName(std::string(GameSupport::getTextPlayLayer(EPlayLayer::kPlayLayer_Select)))->setVisible(false);
    m_PlaySelectLayer->setVisible(false);
    

	m_CongratulationsLayer = node->getChildByName(std::string(GameSupport::getTextPlayLayer(EPlayLayer::kPlayLayer_Congratulation)));
	//Button* btnReplay = dynamic_cast<Button*>(m_CongratulationsLayer->getChildByTag(ECongraLayerTag::btnReplay));
	//btnReplay->addClickEventListener(CC_CALLBACK_0(PlayManager::initialize, this));
	//Button* btnNext = dynamic_cast<Button*>(m_CongratulationsLayer->getChildByTag(ECongraLayerTag::btnNext));
	//btnNext->addClickEventListener(CC_CALLBACK_0(PlayManager::ReturnLevelScene, this));


	m_ResultLayer = node->getChildByName(std::string(GameSupport::getTextPlayLayer(EPlayLayer::kPlayLayer_Result)));

	m_CongratulationsLayer->setVisible(false);
	m_ResultLayer->setVisible(false);
	node->setAnchorPoint(Vec2(0.5f, 0.5f));

	node->setPosition(m_screenSize.width / 2, m_screenSize.height / 2);

	this->addChild(node);
}

void PlayManager::OpenPlaySelectLayer()
{
	m_PlaySelectLayer->setVisible(true);
	m_PlayAdjustLayer->setVisible(false);
	m_CongratulationsLayer->setVisible(false);
	m_ResultLayer->setVisible(false);
	initGame();
	initGhost();
    m_EGameStatus = EGameStatus::kGhostStatus_None;
	//GamePlayConfig::GetInstance()->m_selectNumber = -1;
	m_currStar = 3;
	this->scheduleUpdate();
}

void PlayManager::OpenPlayAdjustLayer()
{
	m_PlayAdjustLayer->setVisible(true);
	m_PlaySelectLayer->setVisible(false);
	m_CongratulationsLayer->setVisible(false);
	m_ResultLayer->setVisible(false);
	//initGame();
	//initGhost();
    m_EGameStatus = EGameStatus::kGhostStatus_None;
	//GamePlayConfig::GetInstance()->m_selectNumber = -1;
	m_currStar = 3;
	this->scheduleUpdate();
}

void PlayManager::initGhost()
{
	//ghostPanel->setPosition(-m_screenSize.width/2,m_screenSize.height/2);
	//ghostPanel->setPosition(0,m_screenSize.height/2);

	//ghostPanel->setTag(100);
	// m_PlayLayer->setPosition(-m_screenSize.width/5,m_PlayLayer->getPosition().y);

	

    if (s_playType == EPlayType::kType_Adjust)
	{
        auto str = cocos2d::String::createWithFormat("%s%d", GameSupport::getTextPlayLayer(EPlayLayer::kPlayLayer_Ghost),
			GameData::getInstance()->easyMode.at(LevelManager::s_ID).NumGhostStart)->getCString();
		//m_nodeGhostPlay = m_PlayAdjustLayer->getChildByName("nodeNormal")->getChildByName(str);
    
        //m_PlayAdjustLayer->initGame(m_nodeGhostPlay);
	}
	else
	{
       
		auto str = cocos2d::String::createWithFormat("%s%d", GameSupport::getTextPlayLayer(EPlayLayer::kPlayLayer_Ghost),
			GameData::getInstance()->easyMode.at(LevelManager::s_ID).NumGhostStart)->getCString();
		//m_nodeGhostPlay = m_PlaySelectLayer->getChildByName("nodeNormal")->getChildByName(str);
		switch (LevelManager::s_mode)
		{
		case kMode_Easy:
			m_PlaySelectLayer->initGame(m_nodeGhostPlay, GameData::getInstance()->easyMode.find(LevelManager::s_ID)->second.listExpression.begin()->Answer);
			break;
		case kMode_Normal:
			m_PlaySelectLayer->initGame(m_nodeGhostPlay, GameData::getInstance()->normalMode.find(LevelManager::s_ID)->second.listExpression.begin()->Answer);
			break;
		case kMode_Hard:
			m_PlaySelectLayer->initGame(m_nodeGhostPlay, GameData::getInstance()->hardMode.find(LevelManager::s_ID)->second.listExpression.begin()->Answer);
			break;
		}

	}
}

void PlayManager::initGame()
{
	char s[100];

	std::vector<Expression> listExpression;


	switch (LevelManager::s_mode)
	{
	case kMode_Easy:
		listExpression = GameData::getInstance()->easyMode.find(LevelManager::s_ID)->second.listExpression;
		break;
	case kMode_Normal:
		listExpression = GameData::getInstance()->normalMode.find(LevelManager::s_ID)->second.listExpression;
		break;
	case kMode_Hard:
		listExpression = GameData::getInstance()->hardMode.find(LevelManager::s_ID)->second.listExpression;
		break;
	}

	sprintf(s, "%s", listExpression.begin()->Question);
	//GamePlayConfig::GetInstance()->m_hiddenNumber = listExpression.begin()->Answer;

//	auto label = LabelTTF::create(s, "Arial", m_Clound->getContentSize().height / 3);
//	label->setColor(Color3B::BLACK);
//	label->setTag(EPlayComponent::kPlayComponent_Equation);
//	label->setPosition(m_Clound->getContentSize().width / 2, m_Clound->getContentSize().height / 3);
//
//	m_Clound->addChild(label);
}


void PlayManager::update(float dt)
{
    if (s_playType == EPlayType::kType_Select)
	{
//		if (GamePlayConfig::GetInstance()->m_selectNumber != -1)
//		{
//			m_EGameStatus = GamePlayConfig::GetInstance()->CompareResult() ? EGameStatus::Correct : EGameStatus::Fail;
//		}
//		if (m_EGameStatus == EGameStatus::Correct || m_EGameStatus == EGameStatus::Fail)
//		{
//			this->unscheduleUpdate();
//			OpenResultLayer();
//		}
//		else if (m_EGameStatus == EGameStatus::None)
//		{
//			if (m_loadingBar->getPercent() == 0)
//			{
//				m_EGameStatus = EGameStatus::Fail;
//			}
//		}
	}

}

void PlayManager::OpenResultLayer()
{
	m_PlaySelectLayer->setVisible(false);
	m_PlayAdjustLayer->setVisible(false);
	m_CongratulationsLayer->setVisible(false);

	initResult();
	m_ResultLayer->setVisible(true);

	auto func = CallFuncN::create(CC_CALLBACK_0(PlayManager::OpenCongratulationsLayer, this));
	m_ResultLayer->runAction(Sequence::create(DelayTime::create(3.0f), func, NULL));
}

void PlayManager::initResult()
{
	LabelTTF* label = (LabelTTF*)m_Clound->getChildByTag(EPlayComponent::kPlayComponent_Equation);
	std::string equation = label->getString();
	PlayResultLayer* layer = new PlayResultLayer(m_screenSize);
	layer->setPosition(Vec2::ZERO);
	//layer->setTag(EPlayLayerTag::DisplayResult);
	layer->autorelease();
	this->addChild(layer);

	//layer->initalize(equation, m_EGameStatus);

}

void PlayManager::OpenCongratulationsLayer()
{
	m_ResultLayer->setVisible(false);
	//this->getChildByTag(EPlayLayerTag::DisplayResult)->removeFromParent();
	initCongratulations();
	m_CongratulationsLayer->setVisible(true);
}

void PlayManager::initCongratulations()
{
//	if (m_currStar < 2)
//	{
//		Sprite* star3 = dynamic_cast<Sprite*>(m_CongratulationsLayer->getChildByTag(ECongraLayerTag::sprStar3));
//		star3->setVisible(false);
//	}
//	if (m_currStar < 1)
//	{
//		Sprite* star2 = dynamic_cast<Sprite*>(m_CongratulationsLayer->getChildByTag(ECongraLayerTag::sprStar2));
//		star2->setVisible(false);
//
//	}
//	if (m_currStar < 0)
//	{
//		Sprite* star2 = dynamic_cast<Sprite*>(m_CongratulationsLayer->getChildByTag(ECongraLayerTag::sprStar2));
//		star2->setVisible(false);
//
//	}
//	LabelTTF* label = (LabelTTF*)m_Clound->getChildByTag(EPlayComponent::Equation);
//	std::string equation = label->getString();
//	equation.replace(equation.find("?"), 1, String::createWithFormat("%d", GamePlayConfig::GetInstance()->m_selectNumber)->getCString());
//
//	LabelTTF* newLabel = LabelTTF::create(equation, "Arial", 25);
//	newLabel->setColor(Color3B::WHITE);
//
//	//   newLabel->setScale(kScale);
//
//	Sprite* sumbaryBox = (Sprite*)m_CongratulationsLayer->getChildByTag(ECongraLayerTag::SumaryBox);
//	newLabel->setPosition(sumbaryBox->getContentSize().width / 2, sumbaryBox->getContentSize().height / 2);
//	sumbaryBox->addChild(newLabel);
//
//
//	//Save data play
//	if (m_EGameStatus == EGameStatus::Correct)
//	{
//		PlayerInfo::getInstance()->level->setLevelEnable(LevelManager::m_ELevelMode, LevelManager::currID + 1, true);
//	}
}



void PlayManager::ReturnLevelScene()
{
	//Director::getInstance()->replaceScene(TransitionFade::create(0.5f, LevelManager::createScene(m_ELevelMode)));
}